<?php

class MessageController extends Controller {
    protected $allowed = array('administrator' , 'member');

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        if ($this->isAllowed()) {
            $messages_temp = Model::getRowsFromQuery("SELECT message_id, status FROM message_user WHERE user_id = ".$this->auth->id.";");

            $messages = array();
            foreach ($messages_temp as $message){
                $message = Model::factory('message', $message['message_id']);
                //$message["id"] = $message["message_id"];

                $messages[] = $message;
            }

            if (isset($_GET['ajax']))
                $this->render('message/ajax_index' , array('messages' => $messages));
            else
                $this->render('message/index' , array('messages' => $messages));
        }
        else {
            $this->showError('You are not allowed to open this page');
        }
    }

    public function compose() {
        if ($this->isAllowed()) {
            if ($_POST) {
                $message = Model::factory('message');
                $message->subject = $_POST['subject'];
                $message->save();

                $mb = Model::factory('messagebody');
                $mb->message_id = $message->id;
                $mb->sender_id = $this->auth->id;
                $mb->body = $_POST['body'];
                $mb->save();

                foreach ($_POST['tags'] as $key => $val) {
                    $user = Model::factory('auth' , $key);
                    $message->addUserInvolved($user);
                }
                $message->addUserInvolved($this->auth);
                $this->redirect($this->createUrl('message/index'));
            }
        }
        else {
            $this->showError('You are not allowed to open this page');
        }
    }

    public function ajaxview($message_id) {
        if ($this->isAllowed()) {
            $message = Model::factory('message', $message_id);
            if ($message->loaded()) {
                $bodies = $message->messagebody;
                $page = (isset($_GET['page'])) ? $_GET['page'] : 1;

                $bodies = Model::paginaze($bodies, $page);
                
                /*$this->render('message/view' , array(
                    'message'   => $message,
                    'bodies'    => $bodies)
                );*/

                foreach($bodies as $body) {
                    $user = $body->getSender();
                    ?>
                    <div class="myWallCommentPhoto"><img src="<?php if($user->getProfilePicture() != NULL) echo $user->getProfilePicture()->getUrl();?>" alt="" /></div>
                    <div class="myWallCommentPoster"><a href="<?php echo $this->createUrl('profile/view/' . $user->id);?>"> <?php echo $user->fullname;?></a></div>
                    <div class="myWallCommentContent"><?php echo $body->body;?></div>
                    <?php
                }
            }
        }
    }

    public function view($message_id) {
        if ($this->isAllowed()) {
            $message        = Model::factory('message', $message_id);
            if ($message->loaded()) {
                if ($_POST) {
                    $newBody = Model::factory('messagebody');
                    $newBody->body = $_POST['body'];
                    $newBody->message_id = $message->id;
                    $newBody->sender_id = $this->auth->id;
                    $newBody->save();
                }

                $bodies         = $message->messagebody;
                $page = (isset($_GET['page'])) ? $_GET['page'] : 1;

                $p = new Pagination(count($bodies), $this->createUrl("message/ajaxview/" . $message->id));
                
                $bodies = Model::paginaze($bodies, $page);
                $users = array();

                /*foreach ($user_involved as $user){
                    $users[$user["user_id"]] = Model::factory('auth', $user["user_id"]);
                }*/

                $this->render('message/view' , array(
                    'message'   => $message,
                    'bodies'    => $bodies,
                    'pagin'     => $p)
                );
            }
            else
                $this->showError('No message exists');
        }
        else
            $this->showError('You are not allowed to open this page');
    }
}

?>
